package com.yupi.shanoj.controller;

import com.yupi.shanoj.common.BaseResponse;
import com.yupi.shanoj.common.ErrorCode;
import com.yupi.shanoj.common.ResultUtils;
import com.yupi.shanoj.exception.BusinessException;
import com.yupi.shanoj.exception.ThrowUtils;
import com.yupi.shanoj.mapper.CommentsMapper;
import com.yupi.shanoj.model.dto.comment.CommentAddRequest;
import com.yupi.shanoj.model.dto.comment.CommentTree;
import com.yupi.shanoj.model.entity.Comments;
import com.yupi.shanoj.model.entity.User;
import com.yupi.shanoj.service.CommentsService;
import com.yupi.shanoj.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/comment")
@Slf4j
public class CommentController {

    @Resource
    private UserService userService;

    @Resource
    private CommentsService commentsService;

    @Autowired
    private CommentsMapper commentsMapper;


    /**
     * 需求：后台要有增删改查功能
     * 前台要有评论，子评论，查看，赞，踩，回复等功能
     */
    @PostMapping("/add")
    public BaseResponse<Long> addComment(@RequestBody CommentAddRequest commentAddRequest, HttpServletRequest request) {
        if (commentAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Comments comments = new Comments();
        BeanUtils.copyProperties(commentAddRequest, comments);
        User loginUser = userService.getLoginUser(request);
        comments.setUserId(loginUser.getId());

        boolean result = commentsService.sendComment(comments);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        long newPostId = comments.getId();
        return ResultUtils.success(newPostId);
    }

    @PostMapping("/delete")
    public BaseResponse<Long> delete(@RequestParam("id") Integer id) {
        long i = commentsMapper.deleteById(id);
        return ResultUtils.success(i);
    }


    @GetMapping("/get")
    public BaseResponse<List<CommentTree>> getCommentTreeByvid(@RequestParam("itemId") Integer itemId,
                                                               @RequestParam("offset") Long offset,
                                                               @RequestParam("type") Integer type) {
        if(itemId == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(commentsService.getCommentTreeByitemid(itemId,offset,type));
    }

    /**
     * 点赞
     */
    @PostMapping("/like-or-dislike")
    public BaseResponse<Long> likeOrDislike(@RequestParam("mid") Integer mid,
                                              @RequestParam("uid") Integer uid) {
        if(mid == null || uid == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(commentsService.likeOrDislike(mid,uid));
    }

}
